package com.gd.deron.smartrobot.baobao.service;



import com.gd.deron.smartrobot.baobao.app.MyApplication;
import com.gd.deron.smartrobot.baobao.utils.BroadCastUtils;
import com.gd.deron.smartrobot.baobao.utils.Config;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUnderstander;
import com.iflytek.cloud.SpeechUnderstanderListener;
import com.iflytek.cloud.UnderstanderResult;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class AsrService extends Service {
	protected static final String TAG = "AsrService";
	private BLEBinder ibinder = new BLEBinder();
	// 语义理解对象（语音到语义）
	private SpeechUnderstander mSpeechUnderstander;
	// 通过服务给单片机发送消息
	private String language = "zh_cn";// 设置语言
	private String accent = "mandarin";// 设置语言区域

	@Override
	public void onCreate() {
		super.onCreate();
		mSpeechUnderstander = SpeechUnderstander.createUnderstander(this, speechUnderstanderListener);
		setParam() ;
	}
	public void setParam() {

		mSpeechUnderstander.setParameter(SpeechConstant.DOMAIN, "iat");
		mSpeechUnderstander.setParameter(SpeechConstant.NLP_VERSION, "2.0");
		mSpeechUnderstander.setParameter(SpeechConstant.RESULT_TYPE, "json");
		// 设置语言
		mSpeechUnderstander.setParameter(SpeechConstant.LANGUAGE, language);
		// 设置语言区域
		mSpeechUnderstander.setParameter(SpeechConstant.ACCENT, accent);
		// 设置语音前端点
		mSpeechUnderstander.setParameter(SpeechConstant.VAD_BOS, "4000");
		// 设置语音后端点
		mSpeechUnderstander.setParameter(SpeechConstant.VAD_EOS, "1000");
		// 设置标点符号
		mSpeechUnderstander.setParameter(SpeechConstant.ASR_PTT, "1");
	}
	/**
	 * 初始化监听器（语音到语义）。
	 */
	private InitListener speechUnderstanderListener = new InitListener() {
		@Override
		public void onInit(int code) {
			Log.d(TAG, "speechUnderstanderListener init() code = " + code);
			if (code != ErrorCode.SUCCESS) {
				//showTip("初始化失败,错误码：" + code);
			}
		}
	};

	int ret = 0;// 函数调用返回值
	
	/** 开始语音语义理解 */
	public void startUnderstanding() {
		if (mSpeechUnderstander.isUnderstanding())
			mSpeechUnderstander.stopUnderstanding();

		ret = mSpeechUnderstander.startUnderstanding(mRecognizerListener);
		if (ret != 0) {
			return;
		}
	}
	public void stopUnderstanding() {
		if (mSpeechUnderstander != null) {
			if (mSpeechUnderstander.isUnderstanding())
				mSpeechUnderstander.stopUnderstanding();
		}
	}
	public void cancelUnderstanding() {
		if (mSpeechUnderstander != null) {
			mSpeechUnderstander.cancel();
		}
	}
	/**
	 * 识别回调。
	 */
	private SpeechUnderstanderListener mRecognizerListener = new SpeechUnderstanderListener() {

		@Override
		public void onResult(final UnderstanderResult result) {
			if (null != result) {
				// 显示回调的结果json格式
				String text = result.getResultString();
				//broadcastSend(Config.ASR_RESULT,text);
				BroadCastUtils.broadcastSend(MyApplication.mAPP,Config.ASR_RESULT,text);
			}
			
		}

		@Override
		public void onEndOfSpeech() {
		}

		@Override
		public void onBeginOfSpeech() {
		}

		@Override
		public void onError(SpeechError error) {
			BroadCastUtils.broadcastSend(MyApplication.mAPP,Config.ASR_ERROR,error.getErrorCode());
			if (mSpeechUnderstander != null) {
				if (mSpeechUnderstander.isUnderstanding())
					mSpeechUnderstander.stopUnderstanding();
				mSpeechUnderstander.cancel();
			}
		}

		@Override
		public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
		}

		@Override
		public void onVolumeChanged(int volume, byte[] arg1) {// 当前音量值，范围[0-30]
			BroadCastUtils.broadcastSend(MyApplication.mAPP,Config.ASR_VOLUME,volume);
		}
	};

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mSpeechUnderstander != null) {
			if (mSpeechUnderstander.isUnderstanding())
				mSpeechUnderstander.stopUnderstanding();
			mSpeechUnderstander.cancel();
			mSpeechUnderstander.destroy();
		}
	}
	@Override
	public IBinder onBind(Intent arg0) {
		return ibinder;
	}
	/**获取类对象*/
	public class BLEBinder extends Binder {
		public AsrService getService() {
			return AsrService.this;
		}
	}
}
